/*
@Author   : Uyynot
@Email    : uyynot@qq.com
@Time     : 2023/8/24 14:49
@File     : list.go
@Project  : web_scaff
@Desc     :
*/
package user

import (
	"fmt"
	"time"
	"web_scaff/models/response"
	"web_scaff/service/user"

	"github.com/gin-gonic/gin"
)

type listRequest struct {
	Page     int    `form:"page"`      // 第几页
	PageSize int    `form:"page_size"` // 每页显示条数
	Username string `form:"username"`  // 用户名
	Phone    string `form:"phone"`     // 手机号
}

type listData struct {
	Uid       int64     `json:"uid"`
	Name      string    `json:"name"`
	Phone     string    `json:"phone"`
	Avatar    string    `json:"avatar"`
	Username  string    `json:"username"`
	CreatedAt time.Time `json:"created_at"`
}

type listResponse struct {
	List       []listData `json:"list"`
	Pagination struct {
		Total    int64 `json:"total"`
		Page     int   `json:"page"`
		PageSize int   `json:"page_size"`
	} `json:"pagination"`
}

func List(c *gin.Context) {
	var req = new(listRequest)
	var res listResponse
	if err := c.ShouldBindQuery(req); err != nil {
		response.FailWithMsg(err.Error(), c)
	}

	aa := 5 / req.Page
	//strconv.Itoa()
	fmt.Println("计算：", aa)
	if req.Page == 0 {
		req.Page = 1
	}
	if req.PageSize == 0 {
		req.PageSize = 5
	}

	//获取结果
	searchData := user.SearchData(*req)
	total, users, err := user.List(&searchData)
	fmt.Println("结果：", total, err, users)
	if err != nil {
		response.FailWithMsg(err.Error(), c)
	}
	//重新封装显示给前端的用户字段
	for _, u := range users {
		res.List = append(res.List, listData{
			Uid:       u.Uid,
			Name:      u.Name,
			Phone:     u.Phone,
			Avatar:    u.Avatar,
			CreatedAt: u.CreatedAt,
		})
	}

	res.Pagination.Total = total
	res.Pagination.Page = req.Page
	res.Pagination.PageSize = req.PageSize
	response.Result(0, res, "success", c)
}
